class_name Arena
extends Node2D


@onready var camera: Camera = $Camera2D


@export_group("boundary")
@export var left_boundary: int
@export var right_boundary: int
@export var top_boundary: int
@export var bottom_boundary: int

@export_group("floating color")
@export var normal_color: Color
@export var critical_color: Color
@export var blocked_color: Color



func _ready():
	_set_camera_limit()
	PlayManager.damage_block.connect(_on_damage_blocked)
	PlayManager.damage_hurt.connect(_on_damage_hurt)

	await get_tree().process_frame

	var player := Sundry.find_player(get_tree())
	player.set_boundaries(left_boundary, right_boundary, top_boundary, bottom_boundary)


func _set_camera_limit():
	camera.limit_left = left_boundary
	camera.limit_right = right_boundary
	camera.limit_top = top_boundary
	camera.limit_bottom = bottom_boundary


# 格挡伤害
func _on_damage_blocked(hurt_uint: Unit, hitbox: HitboxComponent):
	prints("--- _on_damage_blocked ", hurt_uint.name, hitbox.source)

	var text := PlayManager.create_floating_text(hurt_uint.global_position)
	if text:
		get_tree().root.add_child(text)
		text.setup("Blocked!", blocked_color)


# 受到伤害
func _on_damage_hurt(hurt_uint: Unit, hitbox: HitboxComponent):
	prints("--- _on_damage_hurt ", hurt_uint.name, hitbox.source)

	var text := PlayManager.create_floating_text(hurt_uint.global_position)
	if text:
		get_tree().root.add_child(text)
		text.setup(str(hitbox.damage), critical_color if hitbox.critical else normal_color)
